#! usr/bin/env node

const http = require('http'),
      url = require('url'),
      qs = require('querystring'),
      log = console.log;

let items = [];

http.createServer((req,res)=>{
  let path = url.parse(req.url).pathname;

  if(path !== '/'){
    err(res);
    return;
  }

  log(`${req.method} ${req.url} HTTP/${req.httpVersion}`);
  log(req.headers);
  log('');

  switch(req.method){
    case 'GET':
      show(res);
      break;

    case 'POST':
      add(req,res);
      break;

    default:
      err(res);
      break;
  }

 }).listen(8080);

function err(res){
  const msg = 'Not found!';

  res.statusCode = 404;
  res.setHeader('Content-Length',msg.length);
  res.setHeader('Content-Type','text/palin');

  res.end(msg);
}

function add(req,res){
  let body='';
  req.on('data',chunk => body += chunk);
  req.on('end',()=>{
    log(body);

    let item = qs.parse(body).item;
    if(item !== '') items.push(item);

    show(res);
  })

}

function show(res){
  const html = `
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset='UTF-8'>
      <title>Todo list</title>
    </head>
    <body>
      <h1>Todo List</h1>
      <form method="get" action="/">
        <p><input type="text" name="item" /></p>
      </form>
      <ul>
        ${items.map(item => '<li>' + item + '</li>').join('\n')}
      </ul>
    </body>
    </html>`;

  res.setHeader('Content-Type','text/html');
  res.setHeader('Content-Length',Buffer.byteLength(html));

  res.statusCode = 200;
  res.end(html);
}
